﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ResourceEditor.Core;
using System.IO;
using Microsoft.SqlServer.MessageBox;
using ResourceEditor.Core.ResourceDataObjects;

namespace Editor
{
    public partial class EditorBitmap : UserControl
    {
        #region Properties
        private ResourceDataBitmap Data { get; set; }

        private Main MainForm
        {
            get
            {
                return (Main)base.ParentForm;
            }
        }
        #endregion

        #region Ctor
        public EditorBitmap()
        {
            InitializeComponent();
        }
        #endregion

        #region Events
        private void viewThunbanils_Click(object sender, EventArgs e)
        {
            viewInThumbnails.Checked = true;
            viewInDetails.Checked = false;
            viewInList.Checked = false;
            viewInTile.Checked = false;
            lvBitmaps.View = View.LargeIcon;
        }

        private void viewDetails_Click(object sender, EventArgs e)
        {
            viewInThumbnails.Checked = false;
            viewInDetails.Checked = true;
            viewInList.Checked = false;
            viewInTile.Checked = false;
            lvBitmaps.View = View.Details;
            lvBitmaps.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }

        private void viewInList_Click(object sender, EventArgs e)
        {
            viewInThumbnails.Checked = false;
            viewInDetails.Checked = false;
            viewInList.Checked = true;
            viewInTile.Checked = false;
            lvBitmaps.View = View.List;
            lvBitmaps.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void tsTile_Click(object sender, EventArgs e)
        {
            viewInThumbnails.Checked = false;
            viewInDetails.Checked = false;
            viewInList.Checked = false;
            viewInTile.Checked = true;
            lvBitmaps.View = View.Tile;
            lvBitmaps.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void tsBitmapViews_ButtonClick(object sender, EventArgs e)
        {
            if (viewInThumbnails.Checked)
            {
                tsTile_Click(sender, e);
            }
            else if (viewInDetails.Checked)
            {
                viewThunbanils_Click(sender, e);
            }
            else if (viewInList.Checked)
            {
                viewDetails_Click(sender, e);
            }
            else if (viewInTile.Checked)
            {
                viewInList_Click(sender, e);
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            e.Cancel = Main.Manager == null || Main.Current == null || lvBitmaps.SelectedItems.Count == 0;
        }

        private void tsNew_Click(object sender, EventArgs e)
        {
            if (Main.Manager != null && Main.Current != null)
            {
                fdBitmaps.Multiselect = true;
                if (fdBitmaps.ShowDialog() == DialogResult.OK)
                {
                    foreach (var item in fdBitmaps.FileNames)
                    {
                        try
                        {
                            Data.Add(item);
                        }
                        catch (Exception ex)
                        {
                            ExceptionMessageBox box = new ExceptionMessageBox(ex);
                            box.Show(this);
                        }
                    }

                    Fill();
                }
            }
        }

        private void tsDelete_Click(object sender, EventArgs e)
        {
            if (Main.Manager != null && Main.Current != null && lvBitmaps.SelectedItems.Count > 0)
            {
                Data.Delete(lvBitmaps.SelectedItems[0].ImageKey);
                Fill();
            }
        }

        private void tsChange_Click(object sender, EventArgs e)
        {
            if (Main.Manager != null && Main.Current != null)
            {
                fdBitmaps.Multiselect = false;
                if (fdBitmaps.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        Data.ChangeItem(lvBitmaps.SelectedItems[0].Text, fdBitmaps.FileName);
                        Fill();
                    }
                    catch (Exception ex)
                    {
                        ExceptionMessageBox box = new ExceptionMessageBox(ex);
                        box.Show(this);
                    }
                }
            }
        }

        private void tsExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (Main.Manager != null && Main.Current != null && lvBitmaps.SelectedItems.Count > 0)
                {
                    string key = lvBitmaps.SelectedItems[0].Text;
                    saveFileDialog1.FileName = string.Format("{0}.{1}", key, Data.GetExtension(key));

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        Data.Export(key, saveFileDialog1.FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionMessageBox box = new ExceptionMessageBox(ex);
                box.Show(this);
            }
        }

        private void lvBitmaps_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Label) && e.Label != lvBitmaps.SelectedItems[0].Text)
            {
                try
                {
                    Data.ChangeKey(lvBitmaps.SelectedItems[0].Text, e.Label);
                }
                catch (Exception ex)
                {
                    ExceptionMessageBox box = new ExceptionMessageBox(ex);
                    box.Show(this);
                    e.CancelEdit = true;
                }
            }
            else
            {
                e.CancelEdit = true;
            }
        }
        #endregion

        #region Methods
        internal void Clear()
        {
            lvBitmaps.Items.Clear();
            lvBitmaps.LargeImageList.Images.Clear();
            lvBitmaps.SmallImageList.Images.Clear();
        }

        private void Fill()
        {
            Fill(Data);
        }

        internal void Fill(ResourceDataBitmap resource)
        {
            Clear();

            Data = resource;

            if (MainForm.ChangesMode)
            {
                ShowChanges();
            }
            else
            {
                ShowNormal();
            }

            
        }

        private void ShowChanges()
        {
            lvBitmaps.ShowGroups = true;
            toolStrip1.Enabled = false;
            contextMenuStrip1.Enabled = false;

            foreach (var item in Data)
            {
                if (item.State != ItemState.Original)
                {
                    lvBitmaps.Items.Add(new ListViewItem(
                            new ListViewItem.ListViewSubItem[]
                        {
                            new ListViewItem.ListViewSubItem(){Text = item.Key},
                            new ListViewItem.ListViewSubItem(){Text = item.Current.ImageType()},
                            new ListViewItem.ListViewSubItem(){Text = string.Format("{0} x {1}",  item.Current.Size.Width, item.Current.Size.Height)}
                        }, item.Key, lvBitmaps.Groups["lvg" + item.State.ToString()]));

                    lvBitmaps.LargeImageList.Images.Add(item.Key, item.Current);
                    lvBitmaps.SmallImageList.Images.Add(item.Key, item.Current);
                }
            }
        }

        private void ShowNormal()
        {
            lvBitmaps.ShowGroups = false;
            contextMenuStrip1.Enabled = true;
            toolStrip1.Enabled = true;

            foreach (var item in Data)
            {
                if (item.State != ItemState.Deleted)
                {
                    lvBitmaps.Items.Add(new ListViewItem(
                            new ListViewItem.ListViewSubItem[]
                        {
                            new ListViewItem.ListViewSubItem(){Text = item.Key},
                            new ListViewItem.ListViewSubItem(){Text = item.Current.ImageType()},
                            new ListViewItem.ListViewSubItem(){Text = string.Format("{0} x {1}",  item.Current.Size.Width, item.Current.Size.Height)}
                        }, item.Key));

                    lvBitmaps.LargeImageList.Images.Add(item.Key, item.Current);
                    lvBitmaps.SmallImageList.Images.Add(item.Key, item.Current);
                }
            }
        }
        #endregion
    }
}
